package tr.helper.controller.server;

import java.text.DecimalFormat;
import java.util.Random;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class DemoController extends Application {

    // 服务器连接信息
    private static final String HOST = "47.102.137.32";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "1qazxsw2@#901";

    private static final int MAX_DATA_POINTS = 50;
    private static final int MAX_MEMORY = 1024;    // 最大内存，单位MB

    private ObservableList<XYChart.Data<Integer, Integer>> memoryData = FXCollections.observableArrayList();

    private int dataCount = 0;
    private Random random = new Random();

    private Label memoryLabel;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Memory Monitor");

        // 创建X轴和Y轴
        NumberAxis xAxis = new NumberAxis();
        NumberAxis yAxis = new NumberAxis(0, MAX_MEMORY, MAX_MEMORY / 10);

        // 设置 X 轴的自动标记为 false
        xAxis.setAutoRanging(false);

        // 创建区域图表
        AreaChart<Integer, Integer> areaChart = new AreaChart(xAxis, yAxis);
        areaChart.setTitle("Memory Usage");
        areaChart.setAnimated(false);
        areaChart.getData().setAll(createSeries());

        // 创建内存信息标签
        memoryLabel = new Label();
        memoryLabel.setFont(new Font(20));

        // 创建主界面布局
        VBox vbox = new VBox();
        vbox.getChildren().addAll(areaChart, memoryLabel);

        // 创建场景
        Scene scene = new Scene(new Group(vbox), 800, 600);
        primaryStage.setScene(scene);

        // 更新数据和标签
        updateData();
        updateMemoryLabel();

        // 创建动画定时器
        AnimationTimer timer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                updateData();
                updateMemoryLabel();
                updateChart(areaChart);

                // 手动设置 X 轴的 lowerBound
                xAxis.setLowerBound(dataCount - MAX_DATA_POINTS);
                xAxis.setUpperBound(xAxis.getLowerBound() + 100);
            }
        };
        timer.start();

        primaryStage.show();
    }

    // 创建初始数据系列
    private XYChart.Series<Integer, Integer> createSeries() {
        XYChart.Series<Integer, Integer> series = new XYChart.Series<>();
        series.setName("Memory");
        for (int i = 0; i < MAX_DATA_POINTS; i++) {
            series.getData().add(new XYChart.Data<>(i, 0));
        }
        return series;
    }

    // 更新数据
    private void updateData() {
        if (memoryData.size() >= MAX_DATA_POINTS) {
            memoryData.remove(0);
        }
        memoryData.add(new XYChart.Data<>(dataCount++, getRandomMemoryUsage()));
    }

    // 获取随机内存使用量
    private int getRandomMemoryUsage() {
        return random.nextInt(MAX_MEMORY);
    }

    // 更新内存信息标签
    private void updateMemoryLabel() {
        DecimalFormat df = new DecimalFormat("#.##");
        double usedMemory = memoryData.get(memoryData.size() - 1).getYValue();
        double availableMemory = MAX_MEMORY - usedMemory;
        String memoryText = "Used Memory: " + df.format(usedMemory) + " MB\n" +
            "Available Memory: " + df.format(availableMemory) + " MB";

        // 在 JavaFX 应用线程中更新标签
        Platform.runLater(() -> {
            memoryLabel.setText(memoryText);
        });
    }

    // 更新图表
    private void updateChart(AreaChart<Integer, Integer> chart) {
        XYChart.Series<Integer, Integer> series = chart.getData().get(0);
        series.getData().setAll(memoryData);
    }
}

